package club.xyes.zkh.ecommerce.commons.util;

import club.xyes.zkh.ecommerce.commons.entity.*;
import club.xyes.zkh.ecommerce.commons.fn.GetterFunction;
import club.xyes.zkh.ecommerce.commons.fn.SetterFunction;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Create by 郭文梁 2019/4/24 0024 17:59
 * EntityImagePathUtils
 * 实体中一些URL路径处理的相关工具类
 *
 * @author 郭文梁
 * @data 2019/4/24 0024
 */
public class EntityImagePathUtils {
    /**
     * 静态路径正则
     */
    private static final Pattern STATIC_PATTERN = Pattern.compile("^(http|https)://.*$");

    /**
     * 处理路径
     *
     * @param staticPath 静态资源服务器前缀
     * @param filePath   文件路径
     * @param getter     文件名Getter
     * @param setter     文件名Setter
     */
    public static void pretty(String staticPath,
                              String filePath,
                              GetterFunction<String> getter,
                              SetterFunction<String> setter) {

        String filename = getter.apply();
        if (filename == null) {
            return;
        }
        if (isStatic(filename)) {
            return;
        }
        String url = staticPath + filePath + filename;
        setter.apply(url);
    }

    /**
     * 判断路径是否是静态路径
     *
     * @param path 路径
     * @return 是否为静态
     */
    public static boolean isStatic(String path) {
        Matcher matcher = STATIC_PATTERN.matcher(path);
        return matcher.find();
    }

    /**
     * 处理商铺Logo图片路径为全静态路径
     *
     * @param shop       商铺对象
     * @param staticPath 静态服务器地址
     * @param filePath   文件路径
     * @return Shop
     */
    public static Shop prettyLogoPath(Shop shop, String staticPath, String filePath) {
        if (shop == null) {
            return null;
        }
        pretty(staticPath, filePath, shop::getLogoUrl, shop::setLogoUrl);
        return shop;
    }

    /**
     * 处理商铺Logo图片路径为全静态路径
     *
     * @param shops      商铺对象
     * @param staticPath 静态服务器地址
     * @param filePath   文件路径
     * @return Shop
     */
    public static List<Shop> prettyLogoPath(List<Shop> shops, String staticPath, String filePath) {
        return shops
                .stream()
                .map(s -> prettyLogoPath(s, staticPath, filePath))
                .collect(Collectors.toList());
    }

    /**
     * 处理商铺会员图片路径为全静态路径
     *
     * @param shopMembership 商铺对象
     * @param staticPath     静态服务器地址
     * @param filePath       文件路径
     * @return shopMembership
     */
    public static ShopMembership prettyShopMembershipImagePath(ShopMembership shopMembership, String staticPath, String filePath) {
        pretty(staticPath, filePath, shopMembership::getImage, shopMembership::setImage);
        return shopMembership;
    }

    /**
     * 处理商铺会员图片路径为全静态路径
     *
     * @param shopMemberships 商铺对象
     * @param staticPath      静态服务器地址
     * @param filePath        文件路径
     * @return ShopMemberships
     */
    public static List<ShopMembership> prettyShopMembershipImagePath(List<ShopMembership> shopMemberships, String staticPath, String filePath) {
        return shopMemberships
                .stream()
                .peek(m -> prettyShopMembershipImagePath(m, staticPath, filePath))
                .collect(Collectors.toList());
    }

    /**
     * 处理优惠券图片路径为全静态路径
     *
     * @param coupon     优惠券
     * @param serverPath 静态服务器地址
     * @param filePath   文件路径
     * @return coupon
     */
    public static Coupon prettyCouponImagePath(Coupon coupon, String serverPath, String filePath) {
        pretty(serverPath, filePath, coupon::getImage, coupon::setImage);
        return coupon;
    }

    /**
     * 处理优惠券图片路径为全静态路径
     *
     * @param coupons    优惠券
     * @param serverPath 静态服务器地址
     * @param filePath   文件路径
     * @return Coupons
     */
    public static List<Coupon> prettyCouponImagePath(List<Coupon> coupons, String serverPath, String filePath) {
        return coupons.stream()
                .peek(c -> pretty(serverPath, filePath, c::getImage, c::setImage))
                .collect(Collectors.toList());
    }

    /**
     * 处理优商铺封面图片路径为全静态路径
     *
     * @param commodity  商品
     * @param serverPath 静态服务器地址
     * @param filePath   文件路径
     * @return Commodity
     */
    public static Commodity prettyCommodityFrontCoverPath(Commodity commodity, String serverPath, String filePath) {
        pretty(serverPath, filePath, commodity::getFrontCover, commodity::setFrontCover);
        return commodity;
    }

    /**
     * 处理优商铺封面图片路径为全静态路径
     *
     * @param commodities 商品
     * @param serverPath  静态服务器地址
     * @param filePath    文件路径
     * @return Commodities
     */
    public static List<Commodity> prettyCommodityFrontCoverPath(List<Commodity> commodities, String serverPath, String filePath) {
        return commodities.stream()
                .peek(c -> prettyCommodityFrontCoverPath(c, serverPath, filePath))
                .collect(Collectors.toList());
    }

    /**
     * 处理优商铺图片路径为全静态路径
     *
     * @param shopImage  商品
     * @param serverPath 静态服务器地址
     * @param filePath   文件路径
     * @return ShopImage
     */
    public static ShopImage prettyShopImagePath(ShopImage shopImage, String serverPath, String filePath) {
        if (!shopImage.getAbsolutely()) {
            pretty(serverPath, filePath, shopImage::getImageUrl, shopImage::setImageUrl);
        }
        return shopImage;
    }

    /**
     * 处理优商铺图片路径为全静态路径
     *
     * @param shopImages 商品
     * @param serverPath 静态服务器地址
     * @param filePath   文件路径
     * @return ShopImage
     */
    public static List<ShopImage> prettyShopImagePath(List<ShopImage> shopImages, String serverPath, String filePath) {
        return shopImages.stream().peek(i -> prettyShopImagePath(i, serverPath, filePath)).collect(Collectors.toList());
    }

    /**
     * 处商铺活动图片路径为全静态路径
     *
     * @param shops                  商铺列表
     * @param serverPrefix           静态服务器地址
     * @param shopPromotionImagePath 文件路径
     * @return ShopImage
     */
    public static List<Shop> prettyShopPromotionImagePath(List<Shop> shops, String serverPrefix, String shopPromotionImagePath) {
        return shops.stream().peek(v -> prettyShopPromotionImagePath(v, serverPrefix, shopPromotionImagePath)).collect(Collectors.toList());
    }

    public static Shop prettyShopPromotionImagePath(Shop shop, String serverPrefix, String path) {
        if (shop == null) {
            return null;
        }
        pretty(serverPrefix, path, shop::getPromotionImage, shop::setPromotionImage);
        return shop;
    }
}
